if not HST then return end

function HST:BarInit()
	self.Bar = CreateFrame("Frame", "HSTBarFrame", UIParent)
	self.Bar:SetFrameStrata("MEDIUM")
	self.Bar:RegisterForDrag("LeftButton")
	self.Bar:SetClampedToScreen(true)

	self.Bar.Texture = self.Bar:CreateTexture("HSTBarFrameTexture","OVERLAY")

	self.Bar.Background = self.Bar:CreateTexture(nil,"ARTWORK")
	self.Bar.Background:SetTexture(45/100, 45/100, 45/100, 0.6)
	self.Bar.Background:SetAllPoints(self.Bar)

	self.Bar.Border = self.Bar:CreateTexture(nil,"BORDER")
	self.Bar.Border:SetPoint("CENTER",self.Bar,"CENTER")	
	self.Bar.Border:SetTexture(0,0,0)

	self.Bar.BorderBackground = self.Bar:CreateTexture(nil,"BACKGROUND")
	self.Bar.BorderBackground:SetPoint("CENTER",self.Bar,"CENTER")
	self.Bar.BorderBackground:SetTexture(1,1,1)
end

function HST:OnBarProfileEnabled()
    local x = HST.db.profile.x
	local y = HST.db.profile.y
	local height = HST.db.profile.barHeight
	local width = HST.db.profile.width
	local direction = HST.db.profile.direction

	self.Bar:SetAlpha(HST.db.profile.alpha)
	self.Bar:SetWidth(width)
	self.Bar:SetHeight(height)
	self.Bar:ClearAllPoints()

	if x and y then
		self.Bar:SetPoint("TOPLEFT", UIParent, "TOPLEFT", x, y)
	else
		self.Bar:SetPoint("CENTER", UIParent, "CENTER", 0, -350)
	end

	self.Bar.Texture:SetHeight(height)
	self.Bar.Texture:SetWidth(width)
	self.Bar.Texture:SetTexture("Interface\\AddOns\\HST\\media\\"..HST.db.profile.timertexture)

	if self.SwingStart then
		self.Bar.Texture:SetVertexColor(
            HST.db.profile.colors.drawcolor.r,
            HST.db.profile.colors.drawcolor.g,
            HST.db.profile.colors.drawcolor.b)
	else
        self.Bar:SetAlpha(0)
		self.Bar.Texture:SetVertexColor(
            HST.db.profile.colors.timercolor.r,
            HST.db.profile.colors.timercolor.g,
            HST.db.profile.colors.timercolor.b)
	end

	self.Bar.Border:SetWidth(width + 2)
	self.Bar.Border:SetHeight(height + 2)

	self.Bar.BorderBackground:SetWidth(width + (HST.db.profile.border * 1.5))
	self.Bar.BorderBackground:SetHeight(height + (HST.db.profile.border * 1.5))
	self.Bar.BorderBackground:SetVertexColor(
        HST.db.profile.colors.bordercolor.r,
        HST.db.profile.colors.bordercolor.g,
        HST.db.profile.colors.bordercolor.b)

	if direction then
		self.Bar.Texture:SetPoint("LEFT",self.Bar,"LEFT")
	else
		self.Bar.Texture:SetPoint("CENTER",self.Bar,"CENTER")
	end

	self.Bar:Hide()
end

function HST:BarToggleLock(init)
	if not init then
		HST.db.profile.locked = not HST.db.profile.locked
	end

	local Bar = self.Bar
	if HST.db.profile.locked then
		Bar:EnableMouse(nil)
		Bar:SetMovable(nil)
		Bar:SetScript("OnDragStart", nil)
		Bar:SetScript("OnDragStop", nil)
	else
		Bar:EnableMouse(1)
		Bar:SetMovable(1)
		Bar:SetScript("OnDragStart", function() this:StartMoving() end)
		Bar:SetScript("OnDragStop", function()
			this:StopMovingOrSizing()
			local _, _, _, x, y = this:GetPoint()
			HST.db.profile.x = x
			HST.db.profile.y = y
		end)
	end

	if not self.shooting and HST.db.profile.locked then
		Bar:SetAlpha(0)
	elseif self.moving then
		Bar:SetAlpha(HST.db.profile.malpha)
	else
		Bar:SetAlpha(HST.db.profile.alpha)
	end
end

function HST:BarOnEvent(event, arg1)
    if event == "START_AUTOREPEAT_SPELL" then
        HST:HandleStartAutoRepeat()
    elseif event == "STOP_AUTOREPEAT_SPELL" then 
        HST:HandleStopAutoRepeat()
    elseif event == "UNIT_AURA" then
    elseif event == "ITEM_LOCK_CHANGED" then
        HST:HandleBarItemLockChanged()
    elseif event == "UNIT_RANGEDDAMAGE" then
		HST:HandleRangeDamageChanged()
    elseif event == "HST_ON_BAR_UPDATE" then
        HST:HandleBarUpdate()
    end
end

function HST:HandleStartAutoRepeat()
    local curTime = GetTime()
	self.waittime = UnitRangedDamage("player") - self:GetAutoShotCastTime()
	self.shooting = true

	if curTime - self.lastshot < self.waittime then
		self.Bar.Texture:SetVertexColor(
			HST.db.profile.colors.timercolor.r,
			HST.db.profile.colors.timercolor.g,
			HST.db.profile.colors.timercolor.b)
	else
		self.Bar.Texture:SetVertexColor(
			HST.db.profile.colors.drawcolor.r,
			HST.db.profile.colors.drawcolor.g,
			HST.db.profile.colors.drawcolor.b)
		self.SwingStart = curTime
	end

    self.Bar:Show()
	self.Bar:SetAlpha(HST.db.profile.alpha)
    self:SendBarUpdateEvent()
end

function HST:HandleStopAutoRepeat()
    self.shooting = nil
    self.Bar:Hide()
    self.Bar:SetAlpha(0)
    HST:CancelBarUpdateEvent()
end

function HST:HandleBarItemLockChanged()
    if self.shooting then
		self.lastshot = GetTime()
		self.ignoregcd = nil
		self.SwingStart = nil

		if self.newswingtime then
			self.waittime = self.newswingtime
			self.newswingtime = nil
		end

		self.Bar.Texture:SetVertexColor(
			HST.db.profile.colors.timercolor.r,
			HST.db.profile.colors.timercolor.g,
			HST.db.profile.colors.timercolor.b)
	end
end

function HST:HandleBarUpdate()
    local curTime = GetTime()
	local tex = self.Bar.Texture
	local width = self.db.profile.width
    
	if not self.SwingStart and (curTime - self.lastshot) >= self.waittime then
		if self.shooting then
			tex:SetVertexColor(
                HST.db.profile.colors.drawcolor.r,
                HST.db.profile.colors.drawcolor.g,
                HST.db.profile.colors.drawcolor.b)
			self.Bar:SetAlpha(HST.db.profile.alpha)
			self.SwingStart = curTime
		else
			if not self.shooting then
				self:CancelBarUpdateEvent()
			end
			if HST.db.profile.locked then
				self.Bar:SetAlpha(0)
			end
		end
	elseif self.SwingStart then
		if self:PlayerMoving() then
			self.SwingStart = curTime
			tex:SetWidth(width)
			self.Bar:SetAlpha(HST.db.profile.alpha)
		else
			tex:SetWidth(width * math.min(((curTime - self.SwingStart) / HST:GetAutoShotCastTime()),1))
			self.Bar:SetAlpha(HST.db.profile.alpha)
		end
	else
		tex:SetWidth(width * (1 - math.min(((curTime - self.lastshot) / self.waittime),1)))
	end
end

function HST:GetElapsedProcess(TargetTime, StartTime, Duration)
	local r = (TargetTime - StartTime) / Duration
	return math.max(math.min(r, 1), 0)
end

function HST:HandleRangeDamageChanged()
	self.newswingtime = UnitRangedDamage("player") - HST:GetAutoShotCastTime()
end